*********************************************************************
***      	COMBINE OBS AND LONG OUTPATIENT STAYS TOGETHER		  ***
*** Last edited: 12/2/2022                       				  ***
*********************************************************************
clear all


log using "${CodePath}/log/identify_obs_combine_$S_DATE.log", text replace

*--------------------------------------------------
* Load long outpatient and observation stays
*--------------------------------------------------

// Outpatient observation stays from ED
use "${DataPath}/observation/opobsED_claimlevel_0715_year.dta", clear
rename obs obsED
label var obsED "n obs stays, from ED"
rename obs_clm_pmt_amt obsED_clm_pmt_amt
label var obsED_clm_pmt_amt "total pmt obs stays, from ED"

// Outpatient observation stays from any source
merge 1:1 prvdrnum year using "${DataPath}/observation/opobs_claimlevel_0715_year.dta", keep(1 3) nogen


label var obs "n stays obs stays, from any source"

label var obs_clm_pmt_amt "total pmt obs stays, from any source"


merge 1:1 prvdrnum year using "${DataPath}/observation/longopED_claimlevel_0715_year.dta", keep(1 3) nogen
rename longop longopED
label var longopED "n stays outpatient stay LOS == 1, from any source"
rename longop_clm_pmt_amt longopED_clm_pmt_amt
label var longopED_clm_pmt_amt "total pmt outpatient stay LOS == 1, from any source"

// Long outpatient stays from any source

merge 1:1 prvdrnum year using "${DataPath}/observation/longop_claimlevel_0715_year.dta", keep(1 3) nogen

label var longop "n stays outpatient stay LOS == 1, from any source"
label var longop_clm_pmt_amt "total pmt outpatient stay LOS == 1, from any source"

*--------------------------------------------------
* Combine long outpatient and obs stays
*--------------------------------------------------

gen obs_oplong = obs + longop
label var obs_oplong "n stays (obs or long outpatient), any source"

gen obs_oplongED = obsED+longopED
label var obs_oplongED "n stays (obs or long outpatient), from eD"


save "${DataPath}/observation/combined_obs_longop_claimlevel_0715_year.dta", replace

log close